﻿// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// This file is part of CodingEditor.
// Note:	This project is derived from Peter Project
//			(hosted on sourceforge and codeplex)
//
// Copyright (c) 2008-2009, CE Team
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


namespace CE.Main
{
	using System;
	using System.Windows.Forms;

	using Be.Windows.Forms;

	public partial class HexFind : Form
	{
		#region Constructors

		public HexFind()
		{
			InitializeComponent();
			hexBox1.ByteProvider = new DynamicByteProvider(new ByteCollection());
			if (rbString.Checked)
				hexBox1.Enabled = false;
		}

		#endregion Constructors

		#region Methods

		public byte[] GetFindBytes()
		{
			if (rbString.Checked) {
				byte[] res = System.Text.ASCIIEncoding.ASCII.GetBytes(txtString.Text);
				return res;
			} else {
				return ((DynamicByteProvider)hexBox1.ByteProvider).Bytes.GetBytes();
			}
		}

		private void HexFind_Activated(object sender, EventArgs e)
		{
			if (rbString.Checked)
				txtString.Focus();
			else
				hexBox1.Focus();
		}

		private void btnCancel_Click(object sender, EventArgs e)
		{
			this.Close();
		}

		private void btnOK_Click(object sender, EventArgs e)
		{
			if (rbString.Checked && txtString.Text.Length == 0)
				DialogResult = DialogResult.Cancel;
			else if (rbHex.Checked && hexBox1.ByteProvider.Length == 0)
				DialogResult = DialogResult.Cancel;
			else
				DialogResult = DialogResult.OK;
		}

		private void rbHex_CheckedChanged(object sender, EventArgs e)
		{
			txtString.Enabled = rbString.Checked;
			hexBox1.Enabled = !txtString.Enabled;

			if (txtString.Enabled)
				txtString.Focus();
			else
				hexBox1.Focus();
		}

		private void rbHex_Enter(object sender, EventArgs e)
		{
			hexBox1.Focus();
		}

		private void rbString_CheckedChanged(object sender, EventArgs e)
		{
			rbHex_CheckedChanged(sender, e);
		}

		private void rbString_Enter(object sender, EventArgs e)
		{
			txtString.Focus();
		}

		#endregion Methods
	}
}